﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.Main;
using WCYCReader.Models;
using WCYCReader.ShareFolder.ViewModel;
using WCYCReader.StorageHelper;

namespace WCYCReader.Pages
{
    public partial class PCAddressPage : PhoneApplicationPage
    {
        private bool IsUpdate = false;
        private aa smb;

        public PCAddressPage()
        {
            InitializeComponent();
            this.Loaded += PCAddressPage_Loaded;
        }

        void PCAddressPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.SelectedConnection != null)
            {
                this.txtAddressName.Text = App.ViewModel.SelectedConnection.Name;
                this.txtAddressIP.Text = App.ViewModel.SelectedConnection.Host;
                this.txtShareFolder.Text = App.ViewModel.SelectedConnection.Service;
                if (App.ViewModel.SelectedConnection.User != null)
                {
                    this.txtAddressLoginName.Text = App.ViewModel.SelectedConnection.User;
                }
                this.pwdAddressLoginPwd.Password = App.ViewModel.SelectedConnection.Password;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //
          
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            //保存信息
            if (!ValidateAddress())
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtShareFolder.Text.Trim()))
            {
                Prompt.PromptMessage("必须选择一个共享的文件夹！");
                return;
            }

            ConnectionViewModel model =  (App.ViewModel.SelectedConnection != null ? App.ViewModel.SelectedConnection : new ConnectionViewModel());
            model.Name = this.txtAddressName.Text.Trim();
            model.Host = this.txtAddressIP.Text.Trim();
            model.User = this.txtAddressLoginName.Text.Trim();
            model.Password = this.pwdAddressLoginPwd.Password.Trim();
            model.Service = this.txtShareFolder.Text.Trim();
            if (App.ViewModel.SelectedConnection == null)
            {
                App.ViewModel.AddConnection(model);
            }
            App.ViewModel.SaveData();
            NaviService.Navigate(QQReaderPage.EBookIntoPage);
            
        }

        private void btnShowShare_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //测试连接是否正确
            if (!ValidateAddress())
            {
                return;
            }
            //
            this.lstFolders.Visibility = System.Windows.Visibility.Collapsed;
            this.lstFolders.ItemsSource = null;
            this.gridAddress.Visibility = Visibility.Collapsed;
            this.gridFolderFromCompute.Visibility = Visibility.Visible;
            this.txtStatus.Visibility = Visibility.Visible;
            //
            this.smb = new aa();
            this.smb.h(new EventHandler(this.smb_ConnectedCompleted));
            this.smb.i(this.txtAddressIP.Text, this.txtAddressLoginName.Text, this.pwdAddressLoginPwd.Password);
        }

        private bool ValidateAddress()
        {
            if (string.IsNullOrWhiteSpace(this.txtAddressName.Text.Trim()))
            {
                MessageBox.Show("您必须填写一个连接电脑地址的名称！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txtAddressIP.Text.Trim()))
            {
                MessageBox.Show("您必须填写一个连接电脑地址的IP地址！");
                return false;
            }

            //TODO 判断是否已经存在了
            return true;
        }


        private void smb_ConnectedCompleted(object sender, EventArgs e)
        {
            if (this.smb.p())
            {
                this.smb.h(new EventHandler<ay>(this.smb_EnumSharesCompleted));
                this.smb.i();
                return;
            }
            if (!this.smb.l())
            {
                this.gridAddress.Visibility = Visibility.Visible;
                this.gridFolderFromCompute.Visibility = Visibility.Collapsed;
                MessageBox.Show(this.smb.m());
            }
        }

        private void smb_EnumSharesCompleted(object object_0, ay ay_0)
        {
            if (this.smb.m() == null)
            {
                this.lstFolders.ItemsSource = ay_0.a;
                this.lstFolders.Visibility = Visibility.Visible;
                this.txtStatus.Visibility = Visibility.Collapsed;
                return;
            }
            if (!this.smb.l())
            {
                MessageBox.Show(this.smb.m());
            }
        }

        private void lstFolders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lstFolders.SelectedItem != null)
            {
                this.txtShareFolder.Text = this.lstFolders.SelectedItem.ToString();
                this.gridAddress.Visibility = Visibility.Visible;
                this.gridFolderFromCompute.Visibility = Visibility.Collapsed;
            }
        }

        

    }
}